﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FAST
{

    /// <summary>
    /// Stores metadata of folder pair item.
    /// </summary>
    [Serializable()]
    public class FolderPairItem
    {
        private DateTime createdTime;
        private List<DateTime> updatedTimeList;//List of timestamp to remember each time of sync. this time corresponds to a restore point and the time all files are version within that sync.
        private string leftPath;
        private string rightPath;
        private string leftOSPath, rightOSPath;
        private string storagePath;
        private string verPath; //for versionlist path
        private string fpName;


        /// <summary>
        /// Initializes folder pair meta data and make use of folder pair name to generate a unique storage path of serialized data in a folder pair.
        /// </summary>
        /// <param name="fpName"></param>
        public FolderPairItem(string fpName)
        {

            string path = Application.StartupPath;
            string diskLetter = path.Substring(0, path.IndexOf('\\') + 1);

            this.leftPath = "";
            this.rightPath = "";
            this.fpName = fpName;
            this.createdTime = DateTime.Now;
            this.updatedTimeList = new List<DateTime>();
            this.updatedTimeList.Add(createdTime);

            this.leftOSPath = diskLetter + @"\Sessions\Session_" + this.fpName + @"\originalStateLeft_" + this.fpName + ".dat";
            this.rightOSPath = diskLetter + @"\Sessions\Session_" + this.fpName + @"\originalStateRight_" + this.fpName + ".dat";
            this.storagePath = diskLetter + @"\Sessions\Session_" + this.fpName;
            this.verPath = diskLetter + @"\Sessions\versionList.dat";
        }


        public string getFolderPairName() 
        {
            return this.fpName;
        }


        /// <summary>
        /// Gets the created time stamp.
        /// </summary>
        /// <returns>Returns created time stamp of a FolderPairItem</returns>
        public DateTime getCreatedTimeStamp()
        {
            return createdTime;
        }


        /// <summary>
        /// Gets the updated time stamp.
        /// </summary>
        /// <returns>Returns the updated time stamp of a FolderPairItem</returns>
        public DateTime getLastUpdatedTimeStamp()
        {
            return updatedTimeList[updatedTimeList.Count-1];
        }

 
        /// <summary>
        /// Sets FolderPairItem to a time stamp
        /// </summary>
        /// <param name="updatedTime"></param>
        public void setUpdatedDate(DateTime updatedTime)
        {
            updatedTimeList.Add(updatedTime);
        }


        /// <summary>
        /// Gets a unique directory storage path which stores persistent data from that folder pair.
        /// </summary>
        /// <returns>Returns the directory storgae path</returns>
        public string getFolderPairDirectoryStoragePath()
        {
            return this.storagePath;
        }


        /// <summary>
        /// Gets a unique storage path to serialize original state of left folder path.
        /// </summary>
        /// <returns>Returns the left original state path</returns>
        public string getNewOriginalStatePathLeft()//should only be called once, each call generates a new tag (+1). Max number of folder pairs = int range max
        {
            return leftOSPath;
        }

        /// <summary>
        /// Gets a unique storage path to serialize original state of right folder path.
        /// </summary>
        /// <returns>Returns the right original state path</returns>
        public string getNewOriginalStatePathRight()//should only be called once, each call generates a new tag (+1). Max number of folder pairs = int range max
        {
            return rightOSPath;
        }



        /// <summary>
        /// Check if FolderPairItem contains any serialized original state data.
        /// </summary>
        /// <returns>Returns true if original state exists, else false.This value is used to check if there exist a previous sync.</returns>
        public bool isOSExist()
        {
            return File.Exists(leftOSPath);
        }

        /// <summary>
        /// Sets and stores a source and destination folder path to a FolderPairItem.
        /// </summary>
        /// <param name="left">The left folder path</param>
        /// <param name="right">The right folder path</param>
        public void setFolderPaths(string left, string right)
        {
            this.leftPath = left;
            this.rightPath = right;
        }

        /// <summary>
        /// Gets the right folder path.
        /// </summary>
        /// <returns>Returns right folder path of FolderPairItem</returns>
        public string getRightFolderPath()
        {
            return this.rightPath;
        }
        

        /// <summary>
        /// Gets the left folder path.
        /// </summary>
        /// <returns>Returns left folder path of FolderPairItem</returns>
        public string getLeftFolderPath()
        {
            return this.leftPath;
        }

    }
}
